<?php
/**
 * This is Users controller test file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.tests.cases.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
/**
 * UsersControllerTestCase class
 *
 * @package       lil
 * @subpackage    lil.tests.cases.controllers
 */
class UsersControllerTestCase extends ControllerTestCase {
/**
 * fixtures property
 *
 * @var array
 */
	public $fixtures = array(
		'plugin.lil.area', 'plugin.lil.areas_user', 'plugin.lil.user'
	);
/**
 * setUp method
 *
 * @return void
 */
	public function setUp() {
		parent::setUp();
		// reset variables
		require dirname(dirname(dirname(dirname(__FILE__)))) . DS . 'Config' . DS . 'core.php';
	}
/**
 * tearDown method
 *
 * @return void
 */
	public function tearDown() {
		ClassRegistry::flush();
		parent::tearDown();
	}
/**
 * testAdminIndex method
 *
 * @access public
 * @return void
 */
	function testAdminIndex() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array(
				'Session'
			),
			'methods' => array(
				'isAuthorized',
				'redirect'
			),
			'models' => array(
				'User' => array('callPluginHandlers')
			)
		));
		
		// stub out user
		//$this->controller->currentUser = $this->getMock('LilCurrentUser');
		//$this->controller->currentUser->expects($this->any())->method('canAccessArea')->will($this->onConsecutiveCalls(false, true));
		//$this->controller->currentUser->expects($this->any())->method('role')->will($this->onConsecutiveCalls(true, false));
		
		$this->testAction('admin/lil/users/index');
	}
/**
 * testAdminAdd method
 *
 * @return void
 */
	public function testAdminAdd() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
		));
		
		$Users->expects($this->once())->method('setFlash');
		$this->testAction('admin/lil/users/add', array(
			'data' => array(
				'User' => array(
					'admin'    => true,
					'name'     => 'New User From TestSuite',
					'email'    => 'user@testsuite.com',
					'username' => 'testsuite_user',
					'passwd'   => '',
				)
			)
		));
		
		$this->assertFalse(empty($this->headers['Location']));
		
		$expected_url = Router::url(array(
			'admin'      => true,
			'plugin'     => 'lil',
			'controller' => 'users',
			'action'     => 'admin_index'
		), true);
		$this->assertEqual($this->headers['Location'], $expected_url);
		
		$User = ClassRegistry::init('Lil.User');
		$last_name = $User->field('name', array('User.id' => $User->getLastInsertId()));
		$this->assertEqual($last_name, 'New User From TestSuite');
	}
/**
 * testAdminAddWithError method
 *
 * @return void
 */
	public function testAdminAddWithError() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
		));

		$Users->expects($this->once())->method('setFlash')->with($this->anything(), $this->equalTo('error'));
		$this->testAction('admin/lil/users/add', array('data' => array('User' => array('name' => ''))));
		
		$this->assertTrue(empty($this->headers['Location']));
	}
/**
 * testAdminEdit method
 *
 * @return void
 */
	public function testAdminEdit() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
		));
		
		$Users->expects($this->once())->method('setFlash');
		
		$this->testAction('admin/lil/users/edit/1', array(
			'data' => array('User' => array('id' => 1, 'name' => 'John Malkovic'))
		));
		
		$this->assertFalse(empty($this->headers['Location']));
		
		$expected_url = Router::url(array(
			'admin'      => true,
			'plugin'     => 'lil',
			'controller' => 'users',
			'action'     => 'index'
		), true);
		$this->assertEqual($this->headers['Location'], $expected_url);
		
		$User = ClassRegistry::init('Lil.User');
		$last_name = $User->field('name', array('User.id' => 1));
		$this->assertEqual($last_name, 'John Malkovic');
	}
/**
 * testAdminEditUnexistant method
 *
 * @return void
 */
	public function testAdminEditUnexistant() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
			'models' => array('User' => array('callPluginHandlers'))
		));
		
		$Users->expects($this->once())->method('error404');
		$this->testAction('admin/lil/users/edit/-30');
		
	}
/**
 * testAdminEditWithError method
 *
 * @return void
 */
	public function testAdminEditWithError() {
		$Users = $this->generate('Lil.Users', array(
			'loadRoutes' => false,
			'components' => array('Session', 'Auth', 'RequestHandler' => array('isPut')),
			'methods' => array('isAuthorized', 'setFlash', 'error404'),
		));
		
		$Users->expects($this->once())->method('setFlash')->with($this->anything(), $this->equalTo('error'));
		
		$this->testAction('admin/lil/users/edit/1', array(
			'data' => array('User' => array('id' => 1, 'name' => ''))
		));
		
		$this->assertTrue(empty($this->headers['Location']));
		
	}
/**
 * testAdminDelete method
 *
 * @access public
 * @return void
 */
/*	function testAdminDelete() {
		$this->Users->params['controller'] = 'users';
		$this->Users->params['action'] = 'admin_delete';
		$this->Users->params['prefix'] = 'admin';
		$this->Users->Session->write('admin.area', array('Area' =>
		array(
			'id'  => 1,
			'name'  => 'My First Area',
			Configure::read('Lil.areaSlugField') => 'first',
			'description' => 'This is my first area.',
			'theme' => null
		)
		));

		$this->Users->beforeFilter();
		$this->Users->Component->startup($this->Users);
		
		////////////////////////////////
		// 1. check normal delete
		$this->assertTrue($this->Users->User->hasAny(array('User.id' => 1)));

		$this->Users->admin_delete(1);

		//assert that some sort of session flash was set.
		$this->assertTrue($this->Users->Session->check('Message.flash.message'));

		// redirect url must not be empty
		$this->assertFalse(empty($this->Users->test_redirectUrl));

		$this->assertFalse($this->Users->User->hasAny(array('User.id' => 1)));
		
		////////////////////////////////
		// 2. try to delete unexisting
		$this->Users->admin_delete(-30);

		$this->assertTrue($this->Users->Session->check('Message.flash.message'));
		$this->assertFalse(empty($this->Users->test_redirectUrl));
		$this->assertTrue($this->Users->test_404);
	}
/**
 * testIsAuthorized method
 *
 * @access public
 * @return void
 */
/*	function testIsAuthorized() {
		$this->Users->params['controller'] = 'users';
		$this->Users->params['action'] = 'admin_index';

		$this->Users->beforeFilter();
		$this->Users->Component->startup($this->Users);
		
		Configure::write('Lil.allowUsersAnything', true);
		$result = $this->Users->isAuthorized();
		$this->assertTrue($result);
		
		Configure::write('Lil.allowUsersAnything', false);
		$result = $this->Users->isAuthorized();
		$this->assertFalse($result);
		
		// authenticate as user 1
		$this->Users->Auth->login('1');
		$result = $this->Users->isAuthorized();
		$this->assertTrue($result);
		
		// authenticate as user 2 who is not admin
		$this->Users->Auth->login('2');
		$result = $this->Users->isAuthorized();
		$this->assertFalse($result);
		
		$this->Users->params['action'] = 'admin_properties';
		$result = $this->Users->isAuthorized();
		$this->assertTrue($result);
	}
/**
 * testLogin method
 *
 * @access public
 * @return void
 */
/*	function testLogin() {
		$this->Users->params['controller'] = 'users';
		$this->Users->params['action'] = 'login';

		$this->Users->beforeFilter();
		$this->Users->Component->startup($this->Users);
		
		// test2: call login() normally
		$this->Users->login();
		$user = $this->Users->Auth->user();
		$this->assertTrue(empty($user));
		$user = null;
		
		// test2: call login() with user already logged in
		$this->Users->Auth->login('1');
		$this->Users->login();
		$this->assertFalse(empty($this->Users->test_redirectUrl));
		
		$user = $this->Users->Auth->user();
		$this->assertFalse(empty($user));
		$this->Users->Auth->logout();
		
		$user = $this->Users->Auth->user();
		$this->assertTrue(empty($user));
		
		// test3: test remember_me
		$this->Users->Auth->login('1');
		$this->Users->data = array('User' => array(
			'username'    => 'johndoe',
			'passwd'      => Security::hash('admin', null, true),
			'remember_me' => true
		));
		$this->Users->login();
		
		$this->assertFalse(empty($this->Users->test_redirectUrl));
		$user = $this->Users->Auth->user();
		$this->assertFalse(empty($user));
		$cookie = $this->Users->Cookie->read($this->Users->Auth->sessionKey);
		$this->assertFalse(empty($cookie));
		
		$this->Users->Auth->logout();
		
		// test4: test cookies
		$this->Users->data = null;
		$this->Users->login();
		$user = $this->Users->Auth->user();
		$this->assertFalse(empty($user));
		
		$this->Users->Cookie->delete($this->Users->Auth->sessionKey);
	}
/**
 * testLogout method
 *
 * @access public
 * @return void
 */
/*	function testLogout() {
		$this->Users->params['controller'] = 'users';
		$this->Users->params['action'] = 'logout';

		$this->Users->beforeFilter();
		$this->Users->Component->startup($this->Users);
		
		$this->Users->logout();
		$this->assertTrue($this->Users->Session->check('Message.flash.message'));
		$this->assertFalse(empty($this->Users->test_redirectUrl));
	}

/**
 * testAdminProperties method
 *
 * @access public
 * @return void
 */
/*	function testAdminProperties() {
		$this->Users->params['controller'] = 'users';
		$this->Users->params['action'] = 'admin_properties';
		$this->Users->beforeFilter();
		$this->Users->Component->startup($this->Users);
		$this->Users->Auth->login('1');

		$this->Users->test_reset();
		
		$this->Users->data = array('User' => array(
			'id'          => 1,
			'name'        => 'John Doe',
			'email'       => 'john@doe.com',
			'old_pass'    => 'admin',
			'passwd'      => 'newpass',
			'repeat_pass' => 'newpass'
		));
		
		$this->Users->admin_properties();
		
		$this->assertFalse(empty($this->Users->test_redirectUrl));
		
		$data = $this->Users->User->read(null, 1);
		
		$this->assertEqual(Security::hash('newpass', null, true), $data['User']['passwd']);
	}
/**/
}